import request from "../../request/index";
// import {UnwrapNestedRefs} from "vue";

// 手机登录 /login/cellphone?phone=xxx&password=yyy

// /login/qr/check?key=${key}&timestamp=${Date.now()}
export const checkStatus = (key: string) => {
    return request.post('/login/qr/check?key=' + key + '&timestamp=' + Date.now());
}
// /login/status
export const loginStatus = (cookie = '') => {
    return request.post('/login/status?timestamp=' + Date.now(), {
        cookie
    });
}

// /login/qr/key
export const loginQrKey = () => {
    return request.post('/login/qr/key?timestamp=' + Date.now());
}

// /login/qr/create?key=${key}&qrimg=true&timestamp=${Date.now()}
export const loginQrCreate = (key: string) => {
    return request.post('/login/qr/create?key=' + key + '&qrimg=true&timestamp=' + Date.now());
}
// async function checkStatus(key) {
//     const res = await axios({
//         url: `/login/qr/check?key=${key}&timestamp=${Date.now()}`,
//     })
//     return res.data
// }
// async function getLoginStatus(cookie = '') {
//     const res = await axios({
//         url: `/login/status?timestamp=${Date.now()}`,
//         method: 'post',
//         data: {
//             cookie,
//         },
//     })
//     document.querySelector('#info').innerText = JSON.stringify(res.data, null, 2)
// }
// async function login() {
//     let timer
//     let timestamp = Date.now()
//     const cookie = localStorage.getItem('cookie')
//     getLoginStatus(cookie)
//     const res = await axios({
//         url: `/login/qr/key?timestamp=${Date.now()}`,
//     })
//     const key = res.data.data.unikey
//     const res2 = await axios({
//         url: `/login/qr/create?key=${key}&qrimg=true&timestamp=${Date.now()}`,
//     })
//     document.querySelector('#qrImg').src = res2.data.data.qrimg
//
//     timer = setInterval(async () => {
//         const statusRes = await checkStatus(key)
//         if (statusRes.code === 800) {
//             alert('二维码已过期,请重新获取')
//             clearInterval(timer)
//         }
//         if (statusRes.code === 803) {
//             // 这一步会返回cookie
//             clearInterval(timer)
//             alert('授权登录成功')
//             await getLoginStatus(statusRes.cookie)
//             localStorage.setItem('cookie', statusRes.cookie)
//         }
//     }, 3000)
// }
// login()